<?php
class AlipayController extends Core\C{
    public function notifyAction(){
		//计算得出通知验证结果
		$alipayNotify = new \Alipay\Notify(Core\Config::get('alipay'));
		$verify_result = $alipayNotify->verifyNotify();
		//$verify_result = true; //关闭验证
        do{
            //验证失败
		    if(!$verify_result) {
                $ret = false;
                //break;
            }
            //判断该笔订单是否已经做过处理
            $out_trade_no = $this->post('out_trade_no');
            //$otn = explode('-',$out_trade_no);
            $otn = $out_trade_no;
            $orderDB = \OrderModel::where('order_id',$otn[1])->first();
            if($orderDB && $orderDB->pay_status == 1){
                echo "success";exit;
                return ;
			}
            //保存支付宝交易记录
            $order=array();
            $order['out_trade_no'] = $this->post('out_trade_no');
            $order['subject'] = $this->post('subject');
            $order['payment_type'] = $this->post('payment_type');
            $order['trade_no'] = $this->post('trade_no');
            $order['trade_status'] = $this->post('trade_status');
            $order['gmt_create'] = $this->post('gmt_create');
            $order['gmt_payment'] = $this->post('gmt_payment');
            $order['gmt_close'] = $this->post('gmt_close');
            $order['refund_status'] = $this->post('refund_status');
            $order['gmt_refund'] = $this->post('gmt_refund');
            $order['seller_email'] = $this->post('seller_email');
            $order['buyer_email'] = $this->post('buyer_email');
            $order['seller_id'] = $this->post('seller_id');
            $order['buyer_id'] = $this->post('buyer_id');
            $order['price'] = $this->post('price');
            $order['total_fee'] = $this->post('total_fee');
            $order['quantity'] = $this->post('quantity');
            $order['discount'] = $this->post('discount');
            $order['use_coupon'] = $this->post('use_coupon');
            $order['is_total_fee_adjust'] = $this->post('is_total_fee_adjust');
            $order['out_channel_type'] = $this->post('out_channel_type');
            $order['out_channel_amount'] = $this->post('out_channel_amount');
            $order['out_channel_inst'] = $this->post('out_channel_inst');
            $order['business_scene'] = $this->post('business_scene');
            $order['notify_time'] = $this->post('notify_time');
            $order['notify_id'] = $this->post('notify_id');
            $trade_status = $this->post('trade_status'); //交易状态
            if (!($order['trade_status'] == 'TRADE_FINISHED'
                || $order['trade_status'] == 'TRADE_SUCCESS')) {
                    static::log('支付宝交易状态失败');
                    $ret = false;
                    break;
                }
            //交易成功
            $orderDB = \OrderModel::where('order_id',$otn[1])->first();
            if(!$orderDB){
                static::log('没有找到订单');
                $ret = false;
                break;
            }
            if ($order['total_fee'] == $orderDB->pay_need) {
                $orderDB->pay_status = 1;
            } else {
                $orderDB->pay_status = 2;
            }
            if(!$orderDB->save()){
                static::log('支付失败');
                $ret = false;
                break;
            }
            static::log('支付成功');
            echo "success";exit;
            return ;
        } while(1);
        echo 'fail';
        return;
    }
}

